(function () {
    var RewardedVideoAd = function (adUnitId) {

        this.adUnitId = adUnitId;

        this.loadResolve = null;
        this.loadReject = null;
        this.showResolve = null;
        this.showReject = null;

        this.onLoadCallbacks = [];
        this.onErrorCallbacks = [];
        this.onCloseCallbacks = [];

        this.load = function () {
            return new Promise(function (resolve, reject) {
                this.loadResolve = resolve;
                this.loadReject = reject;
                jsb.reflection.callStaticMethod('com/xyzzlky/test/BU', 'loadRewardedVideoAd', '(Ljava/lang/String;)V', adUnitId);
            }.bind(this));
        }
        this.show = function () {
            return new Promise(function (resolve, reject) {
                this.showResolve = resolve;
                this.showReject = reject;
                jsb.reflection.callStaticMethod('com/xyzzlky/test/BU', 'showRewardedVideoAd', '(Ljava/lang/String;)V', adUnitId);
            }.bind(this));
        }
        this.destroy = function () {
            jsb.reflection.callStaticMethod('com/xyzzlky/test/BU', 'destroyRewardedVideoAd', '(Ljava/lang/String;)V', adUnitId);
            rvAd[this.adUnitId] = null;
        }
        this.onLoad = function (callback) {
            this.onLoadCallbacks.push(callback);
        }
        this.offLoad = function (callback) {
            delete this.onLoadCallbacks[callback];
        }
        this.onError = function (callback) {
            this.onErrorCallbacks.push(callback);
        }
        this.offError = function (callback) {
            delete this.onErrorCallbacks(callback);
        }
        this.onClose = function (callback) {
            this.onCloseCallbacks.push(callback);
        }
        this.offClose = function (callback) {
            delete this.onCloseCallbacks(callback);
        }
    };

    var zz = {};

    var rvAd = {};

    zz.loadRVResult = function (adUnitId, res) {
        if (res) {
            rvAd[adUnitId].loadResolve && rvAd[adUnitId].loadResolve();
        } else {
            rvAd[adUnitId].loadReject && rvAd[adUnitId].loadReject();
        }
    }

    zz.showRVResult = function (adUnitId, res) {
        if (res) {
            rvAd[adUnitId].showResolve && rvAd[adUnitId].showResolve();
        } else {
            rvAd[adUnitId].showReject && rvAd[adUnitId].showReject();
        }
    }

    zz.onRVLoad = function (adUnitId) {
        for (var i = 0; i < rvAd[adUnitId].onLoadCallbacks.lenth; i++) {
            rvAd[adUnitId].onLoadCallbacks[i] && rvAd[adUnitId].onLoadCallbacks[i]();
        }
    }

    zz.onRVError = function (adUnitId, errCode, errMsg) {
        for (var i = 0; i < rvAd[adUnitId].onErrorCallbacks.lenth; i++) {
            rvAd[adUnitId].onErrorCallbacks[i] && rvAd[adUnitId].onErrorCallbacks[i]({ errCode: errCode, errMsg: atob(errMsg) });
        }
    }

    zz.onRVClose = function (adUnitId, valid) {
        for (var i = 0; i < rvAd[adUnitId].onCloseCallbacks.lenth; i++) {
            rvAd[adUnitId].onCloseCallbacks[i] && rvAd[adUnitId].onCloseCallbacks[i]({ isEnded: valid });
        }
    }


    zz.createRewardedVideoAd = function (object) {
        var adUnitId = object.adUnitId;
        if (null == adUnitId) {
            return null;
        }
        if (rvAd[adUnitId]) {
            return rvAd[adUnitId];
        }
        var suc = jsb.reflection.callStaticMethod('com/xyzzlky/test/BU', 'createRewardedVideoAd', '(Ljava/lang/String;)Z', adUnitId);
        if (!suc) {
            return null;
        }
        var ad = new RewardedVideoAd(adUnitId);
        rvAd[adUnitId] = ad;
        return ad;
    }
    window.RewardedVideoAd = RewardedVideoAd;
    window.zz = zz;
})();